<?php

namespace app\modules\admin\forms\merchant;

use app\common\forms\BaseForm;
use app\utils\CoordinateUtil;

class MerchantForm extends BaseForm{

    public $merchant_id;
    public $merchant_name;
    public $merchant_desc;
    public $thumbnail;
    public $cat_id;
    public $sort;
    public $customer_type;
    public $lng_lat;

    public function scenarios()
    {
        return [
            'create'=>[
                'merchant_name','merchant_desc','thumbnail','cat_id','sort','customer_type','lng_lat'
            ],
            'update'=>[
                'merchant_id','merchant_name','merchant_desc','thumbnail','cat_id','sort','customer_type','lng_lat'
            ]
        ];
    }

    public function rules()
    {
        return [
            ['merchant_id','required','on'=>['update'],'message'=>\Yii::t("app","ID不能为空")],
            ['merchant_name','required','on'=>['create','update'],'message'=>\Yii::t("app","店铺名称不能为空")],
            ['thumbnail','required','on'=>['create','update'],'message'=>\Yii::t("app","门头照不能为空")],
            ['cat_id','required','on'=>['create','update'],'message'=>\Yii::t("app","分类ID不能为空")],
            ['sort','integer','skipOnEmpty'=>false,'on'=>['create','update'],'message'=>\Yii::t("app","排序错误")],
            ['customer_type','integer','skipOnEmpty'=>false,'on'=>['create','update'],'message'=>\Yii::t("app","客户类型错误")],
            ['lng_lat','required','on'=>['create','update'],'message'=>\Yii::t("app","经纬度不能为空")],
            ['lng_lat','checkLngLat','on'=>['create','update']],
            [['merchant_desc'],'safe','on'=>['create','update']]
        ];
    }

    public function checkLngLat($attribute,$params){
        if(!CoordinateUtil::checkFormat($this->$attribute)){
            $this->addError("lng_lat",\Yii::t("app","经纬度格式错误"));
        }
    }
}